package com.alks.function.service.impl.administration;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.config.handler.AutoCellWriteWidthHandle;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.IdCardUtil;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.enums.chengkong.DefineEnum;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.administration.*;
import com.alks.function.data.request.administration.*;
import com.alks.function.mapper.administration.PcEmployeeMapper;
import com.alks.function.service.administration.PcEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PcEmployeeServiceImpl extends ServiceImpl<PcEmployeeMapper, PcEmployee> implements PcEmployeeService {

    @Resource
    private PcEmployeeMapper pcEmployeeMapper;

    /**
     * 员工资料查询
     *
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getEmployeeInfo(PcEmployeeRequest request) {
        PageRecord<PcEmployeeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeDto> employeeAll = pcEmployeeMapper.getEmployeeAll(request);
        for (PcEmployeeDto t : employeeAll) {
            if (!ObjectUtils.isEmpty(t.getMarried())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getMarried())){
                    t.setMarried("未婚");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getMarried())){
                    t.setMarried("已婚");
                }
            }
            if (!ObjectUtils.isEmpty(t.getChildFlag())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("否");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("是");
                }
            }
            if (!ObjectUtils.isEmpty(t.getStatus())){
                if (t.getStatus().equals("1")){
                    t.setStatus("在职");
                }
                if (t.getStatus().equals("2")){
                    t.setStatus("离职");
                }
            }
        }
        list.setList(employeeAll);
        PageInfo pageInfo= new PageInfo(employeeAll);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 新员工建档-插入员工资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getPcEmployeeAdd(PcEmployeeAddRequest request) {
        if (ObjectUtils.isEmpty(request.getDeptNo())){
            return ResponseInfo.error("请选择部门!");
        }
        if (ObjectUtils.isEmpty(request.getName())){
            return ResponseInfo.error("请输入姓名!");
        }
        if (ObjectUtils.isEmpty(request.getIdNo())){
            return ResponseInfo.error("请输入身份证号码!");
        }
        if (ObjectUtils.isEmpty(request.getPosition())){
            return ResponseInfo.error("请选择职务!");
        }
        if (ObjectUtils.isEmpty(request.getWorkType())){
            return ResponseInfo.error("请选择工种!");
        }
        if (ObjectUtils.isEmpty(request.getDateIn())){
            return ResponseInfo.error("请输入进厂日期!");
        }
        if (ObjectUtils.isEmpty(request.getAddress())){
            return ResponseInfo.error("请输入地址!");
        }
        if (ObjectUtils.isEmpty(request.getMobileNo())){
            return ResponseInfo.error("请输入手机号码!");
        }
        String companyId = UserIdThread.get().getCompanyId();

        if ("BF".equals(companyId)) {
            String idNoErrorMessage = getIdNoErrorMessage(request.getIdNo());
            if (idNoErrorMessage!=null){
                return ResponseInfo.error(idNoErrorMessage);
            }
        }

        String tempExist = getPcEmployeeTempExist(request.getIdNo(),companyId);
        if (tempExist != null) {
            return ResponseInfo.error(tempExist);
        }

        String message = getPcEmployeeExist(request.getIdNo(),companyId);
        if (message!=null){
            return ResponseInfo.error(message);
        }

        PcEmployeeTemp pcEmployeeTemp = new PcEmployeeTemp();
        BeanUtil.copyProperties(request, pcEmployeeTemp);
        pcEmployeeTemp.setCompanyId(companyId);
        pcEmployeeTemp.setSysDate(LocalDateTime.now());
        pcEmployeeTemp.setSysUser(UserIdThread.get().getUserName());

        String workId="";
        //查询工号
        String employeeDelWokeId = pcEmployeeMapper.getEmployeeDelWokeId(companyId);
        String employeeWokeId = pcEmployeeMapper.getEmployeeWokeId(companyId);
        int result = employeeWokeId.compareTo(employeeDelWokeId);
        if (result < 0) {
            workId = getWorkId(employeeDelWokeId,companyId);
            pcEmployeeTemp.setWorkerId(workId);
        } else if (result > 0) {
            workId = getWorkId(employeeWokeId,companyId);
            pcEmployeeTemp.setWorkerId(workId);
        }

        //根据workId判断是否在带确认员工资料中
        String employeeTempWokeId = pcEmployeeMapper.getEmployeeTempWokeId(workId);
        if (!ObjectUtils.isEmpty(employeeTempWokeId)){
            return ResponseInfo.error("工号在待确认员工资料中已存在!");
        }
        pcEmployeeMapper.addPcEmployeeTemp(pcEmployeeTemp);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 根据身份证判断员工临时表是否存在
     * @param idNo
     * @param companyId
     * @return
     */
    public String getPcEmployeeTempExist(String idNo,String companyId) {
        PcEmployeeTemp employeeTemp = pcEmployeeMapper.getEmployeeTemp(idNo,companyId);
        if (!ObjectUtils.isEmpty(employeeTemp)){
            return "身份证号码在待确认员工资料中已存在!";
        }
        return null;
    }

    /**
     *获取工号id
     * @param workId
     * @return
     */
    public static String getWorkId(String workId,String companyId) {
        // 提取字母部分
        String nextId = workId.substring(0, 2);
        // 提取数字部分
        int number = Integer.parseInt(workId.substring(2));
        // 数字加1
        number++;
        // 数字部分补0并拼接到字母部分
        if ("BF".equals(companyId)) {
            nextId += String.format("%06d", number);
        }else {
            nextId += String.format("%04d", number);
        }
        return nextId;
    }

    /**
     * 新员工建档-职务建档列表
     * @return
     */
    @Override
    public ResponseInfo getDutiesList() {
        List<Map<String, String>> dutiesList = pcEmployeeMapper.getDutiesList();
        return ResponseInfo.ok(dutiesList);
    }

    /**
     * 职务建档添加
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getDutiesAdd(Map<String, String> map) {
        if (ObjectUtils.isEmpty(map.get("position"))){
            return ResponseInfo.error("请输入职务!");
        }
        PcEmployeePosition pcEmployeePosition = new PcEmployeePosition();
        String companyId = UserIdThread.get().getCompanyId();
        String userName = UserIdThread.get().getUserName();
        pcEmployeePosition.setCompanyId(companyId);
        pcEmployeePosition.setSysUser(userName);
        pcEmployeePosition.setPosition(map.get("position"));
        pcEmployeePosition.setSysDate(LocalDateTime.now());
        pcEmployeeMapper.addDuties(pcEmployeePosition);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 职务建档删除
     * @param positionSeq
     * @return
     */
    @Override
    public ResponseInfo getDutiesDel(String positionSeq) {
        if (ObjectUtils.isEmpty(positionSeq)){
            return ResponseInfo.error("id不能为空!");
        }
        String position = pcEmployeeMapper.getDuties(positionSeq);
        Integer positionToPcEmployee = pcEmployeeMapper.getPositionToPcEmployee(position);
        if (positionToPcEmployee != null && positionToPcEmployee > 0){
            return ResponseInfo.error("该职务已被引用,不可删除!");
        }
        Integer positionToPcEmployeeTemp = pcEmployeeMapper.getPositionToPcEmployeeTemp(position);
        if (positionToPcEmployeeTemp != null && positionToPcEmployeeTemp > 0){
            return ResponseInfo.error("该职务已被引用,不可删除!");
        }
        pcEmployeeMapper.delDuties(positionSeq);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 职务修改
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getDutiesUpdate(Map<String, String> map) {
        if (ObjectUtils.isEmpty(map.get("positionSeq"))){
            return ResponseInfo.error("id不能为空!");
        }
        if (ObjectUtils.isEmpty(map.get("position"))){
            return ResponseInfo.error("请输入职务!");
        }

        String position = pcEmployeeMapper.getDuties(map.get("positionSeq"));
        Integer positionToPcEmployee = pcEmployeeMapper.getPositionToPcEmployee(position);
        if (positionToPcEmployee != null && positionToPcEmployee > 0){
            return ResponseInfo.error("该职务已被引用,不可修改!");
        }
        Integer positionToPcEmployeeTemp = pcEmployeeMapper.getPositionToPcEmployeeTemp(position);
        if (positionToPcEmployeeTemp != null && positionToPcEmployeeTemp > 0){
            return ResponseInfo.error("该职务已被引用,不可修改!");
        }
        pcEmployeeMapper.updateDuties(map.get("positionSeq"), map.get("position"));
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 工种列表
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getWorkTypeList(PcEmployeeWokeTypeRequest request) {
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeWokeTypeDto> employeeWokeTypeList = pcEmployeeMapper.getEmployeeWokeType(request);
        for (PcEmployeeWokeTypeDto dto : employeeWokeTypeList) {
            if (!ObjectUtils.isEmpty(dto.getWorkTypeCategory())){
                switch (dto.getWorkTypeCategory()){
                    case "0":
                        dto.setWorkTypeCategory("冲裁");
                        break;
                    case "1":
                        dto.setWorkTypeCategory("手艺");
                        break;
                    case "2":
                        dto.setWorkTypeCategory("工艺");
                        break;
                    default:
                        dto.setWorkTypeCategory(null);
                }
            }
        }
        PageRecord<PcEmployeeWokeTypeDto> list = new PageRecord<>();
        list.setList(employeeWokeTypeList);
        PageInfo pageList = new PageInfo(employeeWokeTypeList);
        list.setTotal(pageList.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 工种添加
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getWorkTypeAdd(PcEmployeeWokeTypeAddRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkType())){
            return ResponseInfo.error("请输入工种!");
        }
        if (ObjectUtils.isEmpty(request.getWorkTypeSalary())){
            return ResponseInfo.error("请输入工资!");
        }
        String workTypToPcEmployeeWorkType = pcEmployeeMapper.getWorkTypToPcEmployeeWorkType(request.getWorkType());
        if (!ObjectUtils.isEmpty(workTypToPcEmployeeWorkType)){
            return ResponseInfo.error("该工种已存在,不可重复添加!");
        }
        PcEmployeeWorkType pcEmployeeWorkType = new PcEmployeeWorkType();
        String userName = UserIdThread.get().getUserName();
        String companyId = UserIdThread.get().getCompanyId();
        pcEmployeeWorkType.setCompanyId(companyId);
        pcEmployeeWorkType.setSysUser(userName);
        pcEmployeeWorkType.setSysDate(LocalDateTime.now());
        pcEmployeeWorkType.setWorkType(request.getWorkType());
        pcEmployeeWorkType.setWorkTypeSalary(request.getWorkTypeSalary());
        pcEmployeeWorkType.setWorkTypeCategory(request.getWorkTypeCategory());
        pcEmployeeWorkType.setCalcType(request.getCalcType());
        pcEmployeeMapper.addPcEmployeeWorkType(pcEmployeeWorkType);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 修改工种
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getWorkTypeUpdate(PcEmployeeWokeTypeAddRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkTypeSeq())) {
            return ResponseInfo.error("序号为空不能修改!");
        }
        if (ObjectUtils.isEmpty(request.getWorkTypeCategory())) {
            return ResponseInfo.error("工种类别为空不能修改!");
        }

        //判断是否被引用
        Integer workTypToPcEmployee = pcEmployeeMapper.getWorkTypToPcEmployee(request.getWorkType());
        if (workTypToPcEmployee != null && workTypToPcEmployee > 0) {
            return ResponseInfo.error("该工种已被引用,不可修改!");
        }
        Integer workTypToPcEmployeeTemp = pcEmployeeMapper.getWorkTypToPcEmployeeTemp(request.getWorkType());
        if (workTypToPcEmployeeTemp != null && workTypToPcEmployeeTemp > 0) {
            return ResponseInfo.error("该工种已被引用,不可修改!");
        }
        pcEmployeeMapper.updatePcEmployeeWorkType(request);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 工种删除
     * @param workTypeSeq
     * @return
     */
    @Override
    public ResponseInfo getWorkTypeDel(String workTypeSeq) {
        if (ObjectUtils.isEmpty(workTypeSeq)) {
            return ResponseInfo.error("序号为空不能删除!");
        }
        String workType = pcEmployeeMapper.getPcEmployeeWorkType(workTypeSeq);
        Integer workTypToPcEmployee = pcEmployeeMapper.getWorkTypToPcEmployee(workType);
        if (workTypToPcEmployee != null && workTypToPcEmployee > 0) {
            return ResponseInfo.error("该工种已被引用,不可删除!");
        }
        Integer workTypToPcEmployeeTemp = pcEmployeeMapper.getWorkTypToPcEmployeeTemp(workType);
        if (workTypToPcEmployeeTemp != null && workTypToPcEmployeeTemp > 0) {
            return ResponseInfo.error("该工种已被引用,不可删除!");
        }
        pcEmployeeMapper.delPcEmployeeWorkType(workTypeSeq);
        return ResponseInfo.ok("操作成功!");
    }
    /**
     * 新员工建档-确认员工资料
     * @param idNo
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getConfirmEmployeeInformation(String idNo) {
        if (ObjectUtils.isEmpty(idNo)) {
            return ResponseInfo.error("请选择记录!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        PcEmployeeTemp employeeTemp = pcEmployeeMapper.getEmployeeTemp(idNo,companyId);
        PcEmployee pcEmployee = new PcEmployee();
        BeanUtil.copyProperties(employeeTemp, pcEmployee);
        String userName = UserIdThread.get().getUserName();
        pcEmployee.setSysUser(userName);
        pcEmployee.setSysDate(LocalDateTime.now());
        pcEmployee.setStatus(1);
        pcEmployeeMapper.insert(pcEmployee);
        //写入工资表
        BigDecimal salary = pcEmployeeMapper.getPcEmployeeWorkTypeToSalary(employeeTemp.getWorkType());
        PcBasicSalary pcBasicSalary = new PcBasicSalary();
        BeanUtil.copyProperties(employeeTemp, pcBasicSalary);
        pcBasicSalary.setBasicSalary(salary);
        pcBasicSalary.setSysUser(userName);
        pcBasicSalary.setSysDate(LocalDateTime.now());
        pcEmployeeMapper.addPcBasicSalary(pcBasicSalary);
        pcEmployeeMapper.delPcEmployeeTemp(idNo);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 确认员工资料明细
     * @return
     */
    @Override
    public ResponseInfo getConfirmedEmployeeInformationDetails() {
        String companyId = UserIdThread.get().getCompanyId();
        List<PcEmployeeTempDto> employeeTempDetails = pcEmployeeMapper.getEmployeeTempDetails(companyId);
        for (PcEmployeeTempDto t : employeeTempDetails) {
            if (!ObjectUtils.isEmpty(t.getMarried())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getMarried())){
                    t.setMarried("未婚");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getMarried())){
                    t.setMarried("已婚");
                }
            }
            if (!ObjectUtils.isEmpty(t.getChildFlag())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("否");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("是");
                }
            }
        }
        return ResponseInfo.ok(employeeTempDetails);
    }

    /**
     *待确认员工资料删除
     * @param idNo
     * @return
     */
    @Override
    public ResponseInfo getConfirmedEmployeeInformationDetailsDel(String idNo) {
        if (ObjectUtils.isEmpty(idNo)) {
            return ResponseInfo.error("请选择记录!");
        }
        pcEmployeeMapper.delPcEmployeeTemp(idNo);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 待确认员工资料明细修改
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getConfirmedEmployeeInformationDetailsUpdate(PcEmployeeAddRequest request) {
        if (ObjectUtils.isEmpty(request.getIdNo())) {
            return ResponseInfo.error("请选择记录!");
        }
        PcEmployeeTemp pcEmployeeTemp = new PcEmployeeTemp();
        BeanUtil.copyProperties(request, pcEmployeeTemp);
        String userName = UserIdThread.get().getUserName();
        pcEmployeeTemp.setSysUser(userName);
        pcEmployeeTemp.setCompanyId(UserIdThread.get().getCompanyId());
        pcEmployeeTemp.setSysDate(LocalDateTime.now());
        pcEmployeeMapper.updatePcEmployeeTemp(pcEmployeeTemp);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 员工资料明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getEmployeeInformationDetails(PcEmployeeRequest2 request) {
        PageRecord<PcEmployeeListDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeListDto> pcEmployeeList = pcEmployeeMapper.getPcEmployeeList(request);
        for (PcEmployeeListDto t : pcEmployeeList) {
            if (!ObjectUtils.isEmpty(t.getMarried())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getMarried())){
                    t.setMarried("未婚");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getMarried())){
                    t.setMarried("已婚");
                }
            }
            if (!ObjectUtils.isEmpty(t.getChildFlag())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("否");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("是");
                }
            }
        }
        list.setList(pcEmployeeList);
        PageInfo pageInfo = new PageInfo(pcEmployeeList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 员工资料修改
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getEmployeeInformationUpdate(PcEmployeeUpdateRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerId())) {
            return ResponseInfo.error("工号不存在!");
        }
        if (ObjectUtils.isEmpty(request.getName())){
            return ResponseInfo.error("请输入姓名!");
        }
        if (ObjectUtils.isEmpty(request.getIdNo())){
            return ResponseInfo.error("请输入身份证号码!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        if (ObjectUtils.isEmpty(request.getUpdateStatus())){
            if ("BF".equals(companyId)) {
                String error = getIdNoErrorMessage(request.getIdNo());
                if (error != null){
                    return ResponseInfo.error(error);
                }
            }

            String tempExist = getPcEmployeeTempExist(request.getIdNo(),companyId);
            if (tempExist != null) {
                return ResponseInfo.error(tempExist);
            }

            String message = getPcEmployeeExist(request.getIdNo(),companyId);
            if (message != null) {
                return ResponseInfo.error(message);
            }
        }
        if (ObjectUtils.isEmpty(request.getDateIn())){
            return ResponseInfo.error("请输入进厂日期!");
        }
        if (ObjectUtils.isEmpty(request.getAddress())){
            return ResponseInfo.error("请输入地址!");
        }
        if (ObjectUtils.isEmpty(request.getMobileNo())){
            return ResponseInfo.error("请输入手机号码!");
        }

        PcEmployee pcEmployee = new PcEmployee();
        BeanUtil.copyProperties(request, pcEmployee);
        LambdaUpdateWrapper<PcEmployee> wrapper = new LambdaUpdateWrapper<PcEmployee>()
                .eq(PcEmployee::getWorkerId,request.getWorkerId());
        pcEmployeeMapper.update(pcEmployee, wrapper);
        pcEmployeeMapper.updatePcBasicSalaryCeleType(request.getCalcType(),LocalDateTime.now(),request.getWorkerId());
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 身份证号码校验
     * @param idNo
     * @return
     */
    public String getIdNoErrorMessage(String idNo) {
        if (!ObjectUtils.isEmpty(idNo)){
            boolean idCard = IdCardUtil.isIdcard(idNo);
            if (!idCard){
                return "身份证号码格式不正确!";
            }else {
                int age = IdCardUtil.calculateAgeFromIdCard(idNo);
                if (age < 18){
                    return "年龄小于18岁,不能输入!";
                }
                if (age > 60){
                    return "年龄大于60岁,不能输入!";
                }
            }
        }
        return null;
    }

    /**
     * 根据身份证判断员工资料是否存在
     * @param idNo
     * @return
     */
    public String getPcEmployeeExist(String idNo,String companyId) {
        LambdaQueryWrapper<PcEmployee> pcEmployeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<PcEmployee> queryWrapper = pcEmployeeLambdaQueryWrapper.
                eq(PcEmployee::getIdNo,idNo)
                .eq(PcEmployee::getCompanyId,companyId);
        PcEmployee pcEmployee = pcEmployeeMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(pcEmployee)){
            return "身份证号码在员工资料中已存在!";
        }
        return null;
    }

    /**
     * 根据工号返回数据
     * @param workerId
     * @return
     */
    @Override
    public ResponseInfo getFiling0fRewardAndPunishmentMaterials(String workerId) {
        LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcEmployee::getWorkerId, workerId);
        PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(pcEmployee)){
            PcEmployeeByWorkIdDto pcEmployeeByWorkIdDto = new PcEmployeeByWorkIdDto();
            pcEmployeeByWorkIdDto.setDeptNo(pcEmployee.getDeptNo());
            pcEmployeeByWorkIdDto.setIdNo(pcEmployee.getIdNo());
            pcEmployeeByWorkIdDto.setName(pcEmployee.getName());
            pcEmployeeByWorkIdDto.setDeptName(pcEmployee.getDeptName());
            pcEmployeeByWorkIdDto.setPosition(pcEmployee.getPosition());
            return ResponseInfo.ok(pcEmployeeByWorkIdDto);
        }
        return ResponseInfo.ok(null);
    }

    /**
     * 插入奖惩资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo InsertRewardAndPunishmentInformation(PcEmployeeAwardAddRequest request) {
        if (ObjectUtils.isEmpty(request.getIdNo())){
            return ResponseInfo.error("数据不完整!");
        }
        if (ObjectUtils.isEmpty(request.getDdate())){
            return ResponseInfo.error("请输入奖惩日期!");
        }
        if (ObjectUtils.isEmpty(request.getType())){
            return ResponseInfo.error("请输入奖惩类别!");
        }
        if (ObjectUtils.isEmpty(request.getNums())){
            return ResponseInfo.error("请输入奖惩次数!");
        }
        if (ObjectUtils.isEmpty(request.getReason())){
            return ResponseInfo.error("请输入奖惩原因!");
        }
        PcEmployeeAward pcEmployeeAward = new PcEmployeeAward();
        BeanUtil.copyProperties(request,pcEmployeeAward);
        pcEmployeeAward.setSysDate(LocalDateTime.now());
        String companyId = UserIdThread.get().getCompanyId();
        pcEmployeeAward.setCompanyId(companyId);
        String userName = UserIdThread.get().getUserName();
        pcEmployeeAward.setSysUser(userName);
        pcEmployeeMapper.addPcEmployeeAward(pcEmployeeAward);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 查询奖惩资料
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo SearchForRewardAndPunishmentInformation(PcEmployeeAwardGetRequest request) {
        PageRecord<PcEmployeeAwardDto> list = new PageRecord<>();
        List<PcEmployeeAwardDto> pcEmployeeAwardDtoList = pcEmployeeMapper.getPcEmployeeAward(request);
        PageInfo pageInfo=new PageInfo(pcEmployeeAwardDtoList);
        list.setTotal(pageInfo.getTotal());
        list.setList(pcEmployeeAwardDtoList);
        return ResponseInfo.ok(list);
    }

    /**
     * 删除奖惩资料
     * @param id
     * @return
     */
    @Override
    public ResponseInfo delForRewardAndPunishmentInformation(Integer id) {
        if (ObjectUtils.isEmpty(id)){
            return ResponseInfo.error("参数为空!");
        }
        String userName = UserIdThread.get().getUserName();
        String pcEmployeeAwardExist = pcEmployeeMapper.getPcEmployeeAwardExist(id, userName);
        if (ObjectUtils.isEmpty(pcEmployeeAwardExist)){
            return ResponseInfo.error("不是本人建档不可删除!");
        }
        pcEmployeeMapper.delPcEmployeeAward(id,userName);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 原因建档查询
     * @return
     */
    @Override
    public ResponseInfo getReasonFilingList() {
        String companyId = UserIdThread.get().getCompanyId();
        List<Map<String, String>> list = pcEmployeeMapper.getPcEmployeeDelReason(companyId);
        return ResponseInfo.ok(list);
    }

    /**
     * 原因建档新增
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getReasonFilingAdd(Map<String, String> map) {
        String delSeq = map.get("delSeq");
        String delReason = map.get("delReason");
        if (ObjectUtils.isEmpty(delSeq)){
            return ResponseInfo.error("请输入序号!");
        }
        if (ObjectUtils.isEmpty(delReason)){
            return ResponseInfo.error("请输入原因!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        String pcEmployeeDelReasonByDelSeq = pcEmployeeMapper.getPcEmployeeDelReasonByDelSeq(delSeq, companyId);
        if (!ObjectUtils.isEmpty(pcEmployeeDelReasonByDelSeq)){
            return ResponseInfo.error("序号已存在!");
        }
        String pcEmployeeDelReasonByDelReason = pcEmployeeMapper.getPcEmployeeDelReasonByDelReason(delReason, companyId);
        if (!ObjectUtils.isEmpty(pcEmployeeDelReasonByDelReason)){
            return ResponseInfo.error("离职原因已存在!");
        }
        PcEmployeeDelReason pcEmployeeDelReason = new PcEmployeeDelReason();
        pcEmployeeDelReason.setCompanyId(companyId);
        pcEmployeeDelReason.setDelSeq(delSeq);
        pcEmployeeDelReason.setDelReason(delReason);
        pcEmployeeDelReason.setSysDate(LocalDateTime.now());
        pcEmployeeDelReason.setSysUser(UserIdThread.get().getUserName());
        pcEmployeeMapper.addPcEmployeeDelReason(pcEmployeeDelReason);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 原因建档删除
     * @param delReason
     * @return
     */
    @Override
    public ResponseInfo getReasonFilingDel(String delReason) {
        if (ObjectUtils.isEmpty(delReason)){
            return ResponseInfo.error("参数为空!");
        }
        Integer reasonToPcEmployeeDel = pcEmployeeMapper.getReasonToPcEmployeeDel(delReason);
        if (reasonToPcEmployeeDel != null && reasonToPcEmployeeDel > 0) {
            return ResponseInfo.error("离职资料已有此离职原因,不能删除!");
        }
        String companyId = UserIdThread.get().getCompanyId();
        pcEmployeeMapper.delPcEmployeeDelReason(companyId,delReason);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 部门异动
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo ConfirmationOfChanges(DepartmentalChangesRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerIdList())){
            return ResponseInfo.error("请选择记录!");
        }
        if (ObjectUtils.isEmpty(request.getDeptName())||ObjectUtils.isEmpty(request.getDeptNo())){
            return ResponseInfo.error("请选择新部门!");
        }

        try {
            List<PcEmployeeDeptChg> list = new ArrayList<>();
            for (String t : request.getWorkerIdList()) {
                LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PcEmployee::getWorkerId, t);
                PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
                if (!ObjectUtils.isEmpty(pcEmployee)){
                    PcEmployeeDeptChg pcEmployeeDeptChg = new PcEmployeeDeptChg();
                    BeanUtil.copyProperties(pcEmployee, pcEmployeeDeptChg);
                    pcEmployeeDeptChg.setNewDeptNo(request.getDeptNo());
                    pcEmployeeDeptChg.setNewDeptName(request.getDeptName());
                    pcEmployeeDeptChg.setSysUser(UserIdThread.get().getUserName());
                    pcEmployeeDeptChg.setSysDate(LocalDateTime.now());
                    list.add(pcEmployeeDeptChg);
                }
            }
            if (!ObjectUtils.isEmpty(list)){
                pcEmployeeMapper.addPcEmployeeDeptChg(list);
                pcEmployeeMapper.updateDeptToPcEmployee(request.getDeptNo(),request.getDeptName(),request.getWorkerIdList());
                pcEmployeeMapper.updatePcBasicSalaryToDept(request.getDeptNo(),request.getDeptName(),LocalDateTime.now(),request.getWorkerIdList());
            }
        } catch (Exception e) {
            log.error("ConfirmationOfChanges error: {}", e.getMessage());
            return ResponseInfo.error("操作失败");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 部门异动查询
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo ConfirmationOfChangesList(GetRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> pcEmployeeDeptChgList = pcEmployeeMapper.getPcEmployeeDeptChgList(request);
        list.setList(pcEmployeeDeptChgList);
        PageInfo pageInfo = new PageInfo(pcEmployeeDeptChgList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 职务变更
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo ChangeOfPosition(ChangeOfPositionRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerIdList())) {
            return ResponseInfo.error("请选择记录!");
        }
        if (ObjectUtils.isEmpty(request.getPosition())) {
            return ResponseInfo.error("请选择职务!");
        }
        try {
            List<PcEmployeePositionChg> list = new ArrayList<>();
            for (String t : request.getWorkerIdList()) {
                LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PcEmployee::getWorkerId, t);
                PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
                PcEmployeePositionChg pcEmployeePositionChg = new PcEmployeePositionChg();
                BeanUtil.copyProperties(pcEmployee, pcEmployeePositionChg);
                pcEmployeePositionChg.setNewPosition(request.getPosition());
                pcEmployeePositionChg.setSysUser(UserIdThread.get().getUserName());
                pcEmployeePositionChg.setSysDate(LocalDateTime.now());
                list.add(pcEmployeePositionChg);
            }
            if (!ObjectUtils.isEmpty(list)) {
                pcEmployeeMapper.addPcEmployeePositionChg(list);
                pcEmployeeMapper.updatePositionToPcEmployee(request.getPosition(), request.getWorkerIdList());
                pcEmployeeMapper.updatePcBasicSalaryToPosition(request.getPosition(),LocalDateTime.now(), request.getWorkerIdList());
            }
        } catch (Exception e) {
            log.error("ChangeOfPosition error: {}", e.getMessage());
            return ResponseInfo.error("操作失败");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 职务变更查询
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo ChangeOfPositionList(GetRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> pcEmployeePositionChgList = pcEmployeeMapper.getPcEmployeePositionChgList(request);
        list.setList(pcEmployeePositionChgList);
        PageInfo pageInfo = new PageInfo(pcEmployeePositionChgList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 工种变更
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo JobTypeChange(JobTypeChangeRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerIdList())) {
            return ResponseInfo.error("请选择记录!");
        }
        if (ObjectUtils.isEmpty(request.getWorkType())) {
            return ResponseInfo.error("请选择工种!");
        }
        if (ObjectUtils.isEmpty(request.getSysDate())) {
            return ResponseInfo.error("请选择变更日期!");
        }
        try {
            List<PcEmployeeWorkTypeChg> list = new ArrayList<>();
            int count=0;
            int count2=0;
            for (String t : request.getWorkerIdList()) {
                LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PcEmployee::getWorkerId, t);
                PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
                if (pcEmployee != null) {
                    PcEmployeeWorkTypeChg pcEmployeeWorkTypeChg = new PcEmployeeWorkTypeChg();
                    BeanUtil.copyProperties(pcEmployee, pcEmployeeWorkTypeChg);
                    pcEmployeeWorkTypeChg.setNewWorkType(request.getWorkType());
                    pcEmployeeWorkTypeChg.setSysUser(UserIdThread.get().getUserName());
                    pcEmployeeWorkTypeChg.setSysDate(request.getSysDate());
                    BigDecimal salary = pcEmployeeMapper.getPcEmployeeBasicToSalary(pcEmployee.getWorkerId());
                    if (request.getWorkType().equals(pcEmployee.getWorkType())){
                        count2++;
                    }
                    if ("计时".equals(pcEmployee.getCalcType())){
                        if (salary==null){
                            count++;
                        }
                    }
                    pcEmployeeWorkTypeChg.setOldBasicSalary(salary);
                    pcEmployeeWorkTypeChg.setNewCalcType(pcEmployee.getCalcType());
                    list.add(pcEmployeeWorkTypeChg);
                }
            }
            if (count>0){
                return ResponseInfo.error("存在"+count+"条计时人员的底薪未输入!");
            }
            if (count2>0){
                return ResponseInfo.error("存在"+count2+"条变更前工种与变更后工种相同!");
            }
            if (!ObjectUtils.isEmpty(list)) {
                pcEmployeeMapper.addPcEmployeeWorkTypeChg(list);
                pcEmployeeMapper.updateWorkTypeToPcEmployee(request.getWorkType(), request.getWorkerIdList());
                //修改工种工资
                BigDecimal salary = pcEmployeeMapper.getPcEmployeeWorkTypeToSalary(request.getWorkType());
                pcEmployeeMapper.updateWorkType(salary,LocalDateTime.now(),request.getWorkType(), request.getWorkerIdList());
            }
        } catch (Exception e) {
            log.error("JobTypeChange error: {}", e.getMessage());
            return ResponseInfo.error("操作失败");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 工种变更资料明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo JobTypeChangeList(GetRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> pcEmployeePositionChgList = pcEmployeeMapper.getPcEmployeeWorkTypeChg(request);
        list.setList(pcEmployeePositionChgList);
        PageInfo pageInfo = new PageInfo(pcEmployeePositionChgList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 离职确认作业
     * @param request
     * @return
     */
    @Override
    public ResponseInfo ConfirmationOfResignation(DepartRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerIdList())) {
            return ResponseInfo.error("请选择记录!");
        }
        if (ObjectUtils.isEmpty(request.getDelReason())) {
            return ResponseInfo.error("请选择离职原因!");
        }
        if (ObjectUtils.isEmpty(request.getDelDate())) {
            return ResponseInfo.error("请选择离职日期!");
        }
        try {
            List<PcEmployeeDel> pcEmployeeDelList = new ArrayList<>();
            for (String t : request.getWorkerIdList()) {
                LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PcEmployee::getWorkerId, t);
                PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
                if (pcEmployee != null) {
                    PcEmployeeDel pcEmployeeDel = new PcEmployeeDel();
                    BeanUtil.copyProperties(pcEmployee, pcEmployeeDel);
                    pcEmployeeDel.setDelReason(request.getDelReason());
                    pcEmployeeDel.setDelDate(request.getDelDate());
                    pcEmployeeDel.setSysUser(UserIdThread.get().getUserName());
                    pcEmployeeDel.setSysDate(LocalDateTime.now());
                    pcEmployeeDel.setStatus(2);
                    pcEmployeeDelList.add(pcEmployeeDel);
                }
            }

            if (!pcEmployeeDelList.isEmpty()) {
                pcEmployeeMapper.addPcEmployeeDel(pcEmployeeDelList);
                pcEmployeeMapper.delToPcEmployee(request.getWorkerIdList());
                pcEmployeeMapper.updatePcBasicSalaryToDelReason(request.getDelReason(),request.getDelDate(),request.getWorkerIdList(),LocalDateTime.now());
            }
        } catch (Exception e) {
            log.error("ConfirmationOfResignation error: {}", e.getMessage());
            return ResponseInfo.error("操作失败");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 员工离职资料明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo ConfirmationOfResignationList(GetRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> pcEmployeePositionChgList = pcEmployeeMapper.getPcEmployeeDel(request);
        list.setList(pcEmployeePositionChgList);
        PageInfo pageInfo = new PageInfo(pcEmployeePositionChgList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 离职资料恢复到员工资料中
     * @param idNo
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo ConfirmationOfResignationReduction(String idNo) {
        if (ObjectUtils.isEmpty(idNo)){
            return ResponseInfo.error("请选择记录!");
        }
        PcEmployeeDel pcEmployeeDel = pcEmployeeMapper.getIdNoToPcEmployeeDel(idNo);
        if (!ObjectUtils.isEmpty(pcEmployeeDel)){
            PcEmployee pcEmployee = new PcEmployee();
            pcEmployee.setSysUser(UserIdThread.get().getUserName());
            pcEmployee.setSysDate(LocalDateTime.now());
            pcEmployee.setStatus(1);
            pcEmployee.setCompanyId(UserIdThread.get().getCompanyId());
            pcEmployee.setDeptNo(pcEmployeeDel.getDeptNo());
            pcEmployee.setDeptName(pcEmployeeDel.getDeptName());
            pcEmployee.setName(pcEmployeeDel.getName());
            pcEmployee.setSex(pcEmployeeDel.getSex());
            pcEmployee.setIdNo(pcEmployeeDel.getIdNo());
            pcEmployee.setPosition(pcEmployeeDel.getPosition());
            pcEmployee.setWorkType(pcEmployeeDel.getWorkType());
            pcEmployee.setMobileNo(pcEmployeeDel.getMobileNo());
            pcEmployee.setAddress(pcEmployeeDel.getAddress());
            pcEmployee.setDateIn(pcEmployeeDel.getDateIn());
            pcEmployee.setContract(pcEmployeeDel.getContract());
            pcEmployee.setCalcType(pcEmployeeDel.getCalcType());
            pcEmployee.setWorkerId(pcEmployeeDel.getWorkerId());
            pcEmployee.setBankCardId(pcEmployeeDel.getBankCardId());
            pcEmployee.setBankCardName(pcEmployeeDel.getBankCardName());
            pcEmployee.setBankCardUser(pcEmployeeDel.getBankCardUser());
            pcEmployeeMapper.insert(pcEmployee);
            pcEmployeeMapper.delPcEmployeeDel(idNo);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 离职资料导出
     * @param request
     * @param response
     */
    @Override
    public void ExportResignationAssignment(GetRequest request, HttpServletResponse response) {
        String fileName = "员工离职资料明细_" + DateUtils.dateTimeNow() + ".xlsx";
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeDelDto> list = pcEmployeeMapper.getPcEmployeeDelList(request);
        if (!ObjectUtils.isEmpty(list)) {
            try (InputStream inputStream = new ClassPathResource("templates/员工离职资料模板.xls").getInputStream();
                 OutputStream outputStream = response.getOutputStream();
                 ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).registerWriteHandler(new AutoCellWriteWidthHandle()).build();
            ) {
                ExcelUtils.setResponseZF(response, fileName);
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
                excelWriter.fill(new FillWrapper("t", list), fillConfig, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                log.error("离职资料明细导出失败,ConfirmationOfResignation error: {}", e.getMessage());
                throw new RuntimeException("员工离职资料明细导出失败!");
            }
        }
    }

    /**
     * 银行卡变更信息
     * @param request
     * @return
     */
    @Override
    public ResponseInfo BankCardChange(BankCardChangeRequest request) {
        if (ObjectUtils.isEmpty(request.getWorkerIdList())){
            return ResponseInfo.error("请选择记录!");
        }
        if (ObjectUtils.isEmpty(request.getBankCardId())&&ObjectUtils.isEmpty(request.getBankCardName())&&ObjectUtils.isEmpty(request.getBankCardUser())){
            return ResponseInfo.error("请输入要变更的银行信息!");
        }
        try {
            List<PcEmployeeBankCardChg> list = new ArrayList<>();
            for (String t : request.getWorkerIdList()) {
                LambdaQueryWrapper<PcEmployee> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PcEmployee::getWorkerId, t);
                PcEmployee pcEmployee = pcEmployeeMapper.selectOne(wrapper);
                if (pcEmployee != null) {
                    String brandChange = getBrandChange(pcEmployee.getIdNo());
                    PcEmployeeBankCardChg pcEmployeeBankCardChg = new PcEmployeeBankCardChg();
                    BeanUtil.copyProperties(pcEmployee, pcEmployeeBankCardChg);
                    if (ObjectUtils.isEmpty(brandChange)){
                        pcEmployeeBankCardChg.setAddress("1.2.3.");
                    }else {
                        pcEmployeeBankCardChg.setAddress(brandChange);
                    }
                    pcEmployeeBankCardChg.setSysUser(UserIdThread.get().getUserName());
                    pcEmployeeBankCardChg.setSysDate(LocalDateTime.now());
                    pcEmployeeBankCardChg.setNewBankCard("1."+request.getBankCardUser()+"2."+request.getBankCardName()+"3."+request.getBankCardId());
                    list.add(pcEmployeeBankCardChg);
                }
                if (!ObjectUtils.isEmpty(list)){
                    pcEmployeeMapper.addPcEmployeeBankCardChg(list);
                    pcEmployeeMapper.updateBrandToPcEmployee(request.getWorkerIdList(), request.getBankCardId(), request.getBankCardName(), request.getBankCardUser());
                }
            }
        } catch (Exception e) {
            log.error("BankCardChange error: {}", e.getMessage());
            return ResponseInfo.error("操作失败!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 根据身份证查询上一次变更记录
     * @param idNo
     */
    private String getBrandChange(String idNo) {
        String ChangeInformation = pcEmployeeMapper.getPcEmployeeBankCardChgToIdNo(idNo);
        return ChangeInformation;
    }

    /**
     * 银行卡变更信息列表
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo BankCardChangeList(GetRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> pcEmployeePositionChgList = pcEmployeeMapper.getPcEmployeeBankCardChgList(request);
        list.setList(pcEmployeePositionChgList);
        PageInfo pageInfo = new PageInfo(pcEmployeePositionChgList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 员工职务变更资料明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo EmployeeChangesList(EmployeeChangesRequest request) {
        PageRecord<ChangeDto> list = new PageRecord<>();
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<ChangeDto> EmployeeChangesList = pcEmployeeMapper.getEmployeeChangesList(request);
        list.setList(EmployeeChangesList);
        PageInfo pageInfo = new PageInfo(EmployeeChangesList);
        list.setTotal(pageInfo.getTotal());
        return ResponseInfo.ok(list);
    }

    /**
     * 花名册导出
     * @param request
     * @param response
     */
    @Override
    public void exportPcEmployee(PcEmployeeRequest request, HttpServletResponse response) {
        String fileName = "花名册_" + DateUtils.dateTimeNow() + ".xlsx";
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeDto> employeeAll = pcEmployeeMapper.getEmployeeAll(request);
        if (ObjectUtils.isEmpty(employeeAll)){
            throw new ServiceErrorException("查询数据为空,不能导出!");
        }
        for (PcEmployeeDto t : employeeAll) {
            if (!ObjectUtils.isEmpty(t.getMarried())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getMarried())){
                    t.setMarried("未婚");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getMarried())){
                    t.setMarried("已婚");
                }
            }
            if (!ObjectUtils.isEmpty(t.getChildFlag())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("否");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("是");
                }
            }
            if (!ObjectUtils.isEmpty(t.getStatus())){
                if (t.getStatus().equals("1")){
                    t.setStatus("在职");
                }
                if (t.getStatus().equals("2")){
                    t.setStatus("离职");
                }
            }
            Date birthDay = IdCardUtil.getBirthDay(t.getIdNo());
            if (birthDay!=null) {
                LocalDate localDate = birthDay.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                t.setBrisday(localDate);
            }
            //获取当前日期
            LocalDate currentDate = LocalDate.now();
            if (t.getDelDate()!=null){
                currentDate=t.getDelDate();
            }
            int yearsOfService = 0;
            if (t.getDateIn()!=null) {
                Period period  = Period.between(t.getDateIn(), currentDate);
                yearsOfService = period.getYears();
                t.setSeniority(yearsOfService);
            }else {
                t.setSeniority(0);
            }
        }
        try (InputStream inputStream = new ClassPathResource("templates/员工资料导出模板.xls").getInputStream();
             OutputStream outputStream = response.getOutputStream();
             ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).build();
        ) {
            ExcelUtils.setResponseZF(response, fileName);
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(new FillWrapper("t", employeeAll), fillConfig, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            log.error("花名册导出失败!,exportPcEmployee error: {}", e.getMessage());
            throw new ServiceErrorException("花名册导出失败!");
        }
    }

    /**
     * 员工资料导出
     * @param request
     * @param response
     */
    @Override
    public void exportPcEmployee2(PcEmployeeRequest2 request, HttpServletResponse response) {
        request.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcEmployeeListDto> pcEmployeeList = pcEmployeeMapper.getPcEmployeeList(request);
        String fileName = "花名册_" + DateUtils.dateTimeNow() + ".xlsx";
        if (ObjectUtils.isEmpty(pcEmployeeList)){
            throw new ServiceErrorException("查询数据为空,不能导出!");
        }
        for (PcEmployeeListDto t : pcEmployeeList) {
            if (!ObjectUtils.isEmpty(t.getMarried())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getMarried())){
                    t.setMarried("未婚");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getMarried())){
                    t.setMarried("已婚");
                }
            }
            if (!ObjectUtils.isEmpty(t.getChildFlag())){
                if (DefineEnum.CHECKOUT_FLAG_N.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("否");
                }
                if (DefineEnum.CHECKOUT_FLAG.getValue().equals(t.getChildFlag())){
                    t.setChildFlag("是");
                }
            }
            Date birthDay = IdCardUtil.getBirthDay(t.getIdNo());
            if (birthDay!=null) {
                LocalDate localDate = birthDay.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                t.setBrisday(localDate);
            }
            //获取当前日期
            LocalDate currentDate = LocalDate.now();
            Period period  = Period.between(t.getDateIn(), currentDate);
            int yearsOfService = period.getYears();
            t.setSeniority(yearsOfService);
        }
        try (InputStream inputStream = new ClassPathResource("templates/员工资料导出模板2.xls").getInputStream();
             OutputStream outputStream = response.getOutputStream();
             ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).build();
        ) {
            ExcelUtils.setResponseZF(response, fileName);
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(new FillWrapper("t", pcEmployeeList), fillConfig, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            log.error("花名册导出失败!,exportPcEmployee error: {}", e.getMessage());
            throw new ServiceErrorException("花名册导出失败!");
        }
    }
}
